﻿using System.Data.Entity;
using Emz.Core.Models;
using System.Collections.Generic;
using Emz.Core.Models.Enums;

namespace Emz.Core.DataAccess
{

#if DEBUG    
    public class PayrollDatabaseInitializer : DropCreateDatabaseIfModelChanges<PayrollDataContext>
#else
    public class PayrollDatabaseInitializer : CreateDatabaseIfNotExists<PayrollDataContext> 
#endif
    {        
        protected override void Seed(PayrollDataContext context)
        {
            #region  Users            
            var user = new User
            {
                UserName = "admin",
                Password = "1234",
            };
            user.Roles.Add(new Role { RoleName = "administrator" });
            context.Users.Add(user);

            #endregion

            #region Postions
            var positions = new List<Position>()
            {
                new Position{ Code = "ADC" , Description = "ASSISTANT DETACHMENT COMM", Incentive= 52.08,Retirement = 234.37,UniformAllowance = 50.00 },
                new Position{ Code = "G" , Description = "Security / Lady Guard", Incentive= 52.08,Retirement = 234.37,UniformAllowance = 52.08 },
                new Position{ Code = "SO" , Description = "Security Officer", Incentive= 52.08,Retirement = 234.37,UniformAllowance = 52.08 }
            };
            positions.ForEach(p => context.Positions.Add(p));
            
            #endregion

            #region  Clients
            var clients = new List<Client>()
            {
                new Client{ Code = "SM" , FirmName = "Super Mall", RegularOverTime = 100,SpecialOverTime = 150,SpecialHoliday = 100,LegalHoliday = 200, Cola = 100 },
                new Client{ Code = "NEPO" , FirmName= "Nepo Quad" }
            };    
             clients.ForEach(p => context.Clients.Add(p));

            var client = clients[0];
            var position = positions[1];
            var sector = new Sector { Name = "Sector", Position = position, Client= client, RatePerDay = 250, WorkHours = 8, RatePerHour = 31.25 };

            context.Sectors.Add(sector);
            #endregion

            #region  Utilities
            //PhilHealtTable
            var philHealthItems = new List<PhilHealthTable>();
            double start = 4000;
            double incrementer = 100;
            for (int i = 1; i <= 27; i++)
            {
                philHealthItems.Add(
                    new PhilHealthTable { StartRange = start, EndRange = i < 27 ? (start + 999.99) : 0, Base = start, Total = incrementer }
                );
                start = start + 1000;
                incrementer = incrementer + 25;
            }
            philHealthItems.ForEach(x => context.PhilHealthTable.Add(x));

            //SSSTable
            var sssItems = new List<SSSTable>() 
            {
                new SSSTable { StartRange = 1000, EndRange = 1249.99, Base = 1000,Er = 70.7 , Ee = 33.3, Total = 104 },
                new SSSTable { StartRange = 1250, EndRange = 1749.99, Base = 1500, Er = 106, Ee = 50, Total = 156 },
                new SSSTable { StartRange = 1750, EndRange = 2249.99, Base = 2000, Er = 141.3, Ee = 66.7, Total = 208 },
                new SSSTable { StartRange = 2250, EndRange = 2749.99, Base = 2500, Er = 176.7, Ee = 83.3, Total = 260 },
                new SSSTable { StartRange = 3250, EndRange = 3249.99, Base = 3000, Er = 212, Ee = 100, Total = 312 },
                new SSSTable { StartRange = 3750, EndRange = 3749.99, Base = 3500, Er = 247.3, Ee = 116.7, Total = 364 },
                new SSSTable { StartRange = 4250, EndRange = 4249.99, Base = 4000, Er = 282.7, Ee = 133.3, Total = 416 },
                new SSSTable { StartRange = 4750, EndRange = 4749.99, Base = 4500, Er = 318, Ee = 150, Total = 468 }
            
            };
            sssItems.ForEach(x => context.SSSTable.Add(x));
           
            #endregion         

            #region  Employees
            
            Employee newEmployee = new Employee()
            {                
                EmployeeCode = "LSIA-00001",
                FirstName = "Emmanuel",
                MiddleName = "F.",
                LastName = "De Jesus",
                Gender = "Male",
                NickName = "Emz",
                Detachment = sector,
                Position = positions[0],
                PrimarySchoolName = "SFES",
                PrimarySchoolPeriod = "2000-2004",
                SecondarySchoolName = "SSC",
                SecondarySchoolPeriod = "2004-2007",
                TertiarySchoolName = "SPCF",
                TertiarySchoolPeriod = "2007-2012",
                CollegeDegree = "BSIT",
                ATMno = "123456789",
                PlaceOfBirth = "Mabalacat",
                Qualifier = "None",
                Religion= "Catholic"
                
            };
            
            newEmployee.SetStatus((int)EmployedStatus.Active);

            context.Employees.Add(newEmployee);
            #endregion

            #region Pharaphernalia
             var pharaphernalias = new List<Pharaphernalia>()
            {
                new Pharaphernalia{ Description = "LONG SLEEVES", Amount = 360.00},
                new Pharaphernalia{ Description = "BUSH JACKET SET", Amount = 960.00},
                new Pharaphernalia{ Description = "JACKET(BLACK) ", Amount = 1000.00},
            };
             pharaphernalias.ForEach(p => context.Pharaphernalia.Add(p));
            #endregion

             #region Settings
             //SSSTable
             var settings = new List<Settings>() 
            {
                new Settings { SettingName = "COMPANYNAME", SettingValue="LSIA" },
                new Settings { SettingName = "EMPLOYEECODE", SettingValue="LSIA" },
                new Settings { SettingName = "EMPLOYEECODENO", SettingValue="0" }            
            };
             settings.ForEach(x => context.Settings.Add(x));
             #endregion

             #region DTR
            string jsonTimeRecords =
                                @"[
                                  {
                                    ""Day"": 1,
                                    ""TimeIn"": ""2012-04-24T07:00:00"",
                                    ""TimeOut"": ""2012-04-24T09:30:00"",                                    
                                    ""NoOfHours"": 2.5
                                  },
                                  {
                                    ""Day"": 2,
                                    ""TimeIn"": ""2012-04-24T08:00:00"",
                                    ""TimeOut"": ""2012-04-24T10:30:00"",                                    
                                    ""NoOfHours"": 2.5
                                  }
                                ]";
            var newPayrolldate = new PayrollDate
            {
                 Paydate = new System.DateTime(2012, 1, 1),
                 Startdate = new System.DateTime(2012, 1, 1),
                 Enddate = new System.DateTime(2012, 1, 15)
            };
             var dtr = new List<DayTimeRecord>() 
            {
                new DayTimeRecord
                {              
                   PayrollDate = newPayrolldate,
                   Employee = newEmployee,
                   LogTimeRecords = jsonTimeRecords                   
                }
            };
             dtr.ForEach(x => context.DayTimeRecords.Add(x));

             #endregion

             #region  employeeAdjustments
             var newCACreditAdjustment = new Adjustment {  AccountTypeEnum = AccountType.Advance, Code="A1", Description="Cash Advance Additional",  AccountSignEnum = AccountSign.Credit};
             var newCADebitAdjustment = new Adjustment { AccountTypeEnum = AccountType.Advance, Code = "A2", Description = "Cash Advance Deduction", AccountSignEnum = AccountSign.Debit};

             var empAdjustment = new List<EmployeeAdjustment>() 
            {
                new EmployeeAdjustment { Employee =  newEmployee, PayDate = newPayrolldate, Debit = 0, Credit = 1000, IsValid = true, DateAdded = newPayrolldate.Paydate, AdjustmentType = newCACreditAdjustment },
                new EmployeeAdjustment { Employee =  newEmployee, PayDate = newPayrolldate, Debit = 0, Credit = 1000, IsValid = true, DateAdded = newPayrolldate.Paydate, AdjustmentType = newCACreditAdjustment },
                new EmployeeAdjustment { Employee =  newEmployee, PayDate = newPayrolldate, Debit = 500, Credit = 0, IsValid = true, DateAdded = newPayrolldate.Paydate, AdjustmentType = newCADebitAdjustment }
            };
             empAdjustment.ForEach(x => context.EmployeeAdjustments.Add(x));
             #endregion

             newEmployee.PayrollSummaries.Add(
                 new PayrollSummary { Employee = newEmployee, PayRoll = newPayrolldate }
                 );

             context.SaveChanges();
        }

    }
}
